// pages/upload/upload.js
import { submit } from "../../utils/api";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        iconimg:'../../public/discovery-img/selectimg.png',
        imgList:[],
        error:'',
        name:'添加诗词',
        author:'',
        content:'',
        dynasty:''

    },
    //转跳添加诗词的搜索页面
    addpote(){
        wx.navigateTo({
          url: '../search-discovery/search-discovery',
        })
    },

    //提交
    formsubmit: function(e){
        if(this.data.name != '添加诗词'){
            e.detail.value.pote=this.data.name+'/'+this.data.author+'/'+this.data.dynasty
            e.detail.value.pote_con = this.data.content
        }
        
        let img = this.data.imgList
        let openid = wx.getStorageSync('openid')
        e.detail.value.user_id=openid
        if(e.detail.value.title && e.detail.value.text){
            if(img.length==0){
                submit(e.detail.value).then(res=>{
                    wx.showLoading({
                      title: '加载中',
                    })
                    if(res.code == 200){
                        wx.hideLoading()
    
                        wx.showToast({
                          title: '成功',
                          icon:"success",
                          duration:2000,
                          success:()=>{
                            wx.switchTab({
                              url:  '../discovery/discovery',
                            })
                          }
                        })
                    }
                })
            }else{
    
                submit(e.detail.value).then(res=>{
                    wx.showLoading({
                      title: '加载中',
                    })
                    if(res.code == 200){
                        this.up()
                        wx.hideLoading()
    
                        wx.showToast({
                          title: '成功',
                          icon:"success",
                          duration:2000,
                          success:()=>{
                            wx.switchTab({
                                url: '../discovery/discovery',
                              })
                          }
                        })
    
        
                    }
                })
    
    
            }

        }else{
            this.setData({
                error:'请填写内容'
            })
        }

  
    },
    //删除图片
    deleteimg:function(){
        let img = this.data.imgList
        if(img){
            this.setData({
                imgList:[]
            })
        }
    },
    //选择图片
    selectimg:function(){
        wx.chooseMedia({
            count:1,
            mediaType:['image','video'],
            sourceType:['album','camera'],
            maxDuration:30,
            camera:['back','front'],
            success:(res)=>{
                console.log(res)
                this.setData({
                    imgList:res.tempFiles[0]
                })
 
            }
        })
    },

    //上传图片
    up(){
        let openid = wx.getStorageSync('openid')
        wx.uploadFile({
          filePath: this.data.imgList.tempFilePath,
          name: 'file',
          url: 'http://localhost:3000/uploadImage',
          formData:{
            'user_id':openid
          },
          success:(res)=>{
            console.log(res);
          }
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        wx.switchTab({
            url: '../discovery/discovery',
          })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
    
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})